/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.client.homework.HomeworkInfo;
import com.google.zxing.client.homework.QuestionInfo;

public final class TestActivity extends Activity {

  private static final String TAG = TestActivity.class.getSimpleName();
  private static final int GET_HOMWORK_INFO = 1;
  private TextView textView = null;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.test);

    findViewById(R.id.start_button).setOnClickListener(contactListener);
    textView = (TextView)findViewById(R.id.homework_result_tv);
  }

  private final View.OnClickListener contactListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      Intent intent = new Intent(TestActivity.this,
              CaptureActivity.class);
      startActivityForResult(intent, GET_HOMWORK_INFO);
    }
  };

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case GET_HOMWORK_INFO:
          HomeworkInfo homework = (HomeworkInfo) intent.getSerializableExtra("homework");
          String resultStr = "homework info :";

          resultStr += "  Version_";
          resultStr += homework.getVersion();
          resultStr += "  Type_";
          resultStr += homework.getType();

          resultStr += "  Question_";
          for (QuestionInfo qInfo :
                  homework.getQuestionList()) {
            resultStr += "  ID_";
            resultStr += qInfo.getQI();
            resultStr += "  Type_";
            resultStr += qInfo.getQT();
            if (qInfo.getQT() ==0){
              resultStr += "  Answer_";
              if (qInfo.getChoiceResultVec().size() > 0){
                for (Character answer:
                     qInfo.getChoiceResultVec()) {
                  resultStr += "_";
                  resultStr += answer;
                }
              }else {
                resultStr += "get choice question answer failed";
              }
            }else {
              resultStr += "  PicPath_";
              resultStr += qInfo.getPicPath();
            }
          }

          textView.setText(resultStr);
          break;
      }
    }
  }
}
